module.exports = function (shipit) {
  require('shipit-deploy')(shipit);
  shipit.initConfig({
    prod: {
      servers: ['root@121.196.220.250'],
      deployTo: '/tmp/acgn_koa_template',
      branch: 'master'
    },
    test: {
      servers: 'root@172.16.8.209',
      deployTo: '/DataSSD/acgn_koa_template',
      branch: 'master'
    },
    default: {
      workspace: '/tmp/acgn_koa_template', // 本地的临时工作目录
      repositoryUrl: 'http://172.16.2.214:3000/yangchaoguo/acgn_koa_template.git',
      ignores: ['.git', 'node_modules'],
      keepReleases: 2,
      deleteOnRollback: false
    }
  });

  shipit.task('pwd', function () {
    shipit.remote('pwd').then(function (res) {
      shipit.emit('pwd', res);
    }).catch(function (error) {
      console.log(error);
    });
  });

  shipit.task('db:migrate', function () {
    return shipit.remote('pwd');
  });

  shipit.task('nvm', function () {
    shipit.remote('source ~/.nvm/nvm.sh && nvm list').then(function (res) {
      console.log(res['0'].stdout);
    }).catch(function (error) {
      console.log(error);
    });
  });


  shipit.task('test', function () {
    let command = [
      'ls -lah /DataSSD/acgn_koa_template'
    ].join(' ');
    return shipit.remote(command);
  });

  shipit.on('published', function () {
    if (shipit.options.environment === 'test') {
      let command = [
        'source ~/.nvm/nvm.sh',
        '&& cd /DataSSD/acgn_koa_template/current',
        '&& npm install',
        '&& npm run build',
        '&& (pm2 delete zymk_pc || make install)',
        '&& npm run pm2'
      ].join(' ');
      return shipit.remote(command);
    }

    if (shipit.options.environment === 'prod') {
      let command = [
        'source ~/.nvm/nvm.sh',
        '&& cd /tmp/acgn_koa_template/current',
        '&& npm install',
        '&& npm run build',
        '&& cp -rf /tmp/acgn_koa_template/current/* /data/acgn_koa_template',
        '&& (pm2 delete acgn_koa_template || make install)',
        '&& cd /data/acgn_koa_template',
        '&& npm run pm2',
        '&& pm2 save'
      ].join(' ');
      return shipit.remote(command);
    }
  });
};
